/*
 * @Description: 上传身份证和驾驶证 UploadIdCard
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-23 17:35:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-29 09:55:41
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/provider/driver_information.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

class UploadIdCard extends StatefulWidget {
  @override
  _UploadIdCardState createState() => _UploadIdCardState();
}

class _UploadIdCardState extends State<UploadIdCard> {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _cardIdController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _nameController.addListener(() {
      Provider.of<DriverInformation>(context, listen: false)
          .setDriverInformation(driverName: _nameController.text);
    });
    _cardIdController.addListener(() {
      Provider.of<DriverInformation>(context, listen: false)
          .setDriverInformation(idNo: _cardIdController.text);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('身份证与驾驶证'),
        elevation: 0,
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          color: Color(0xffffffff),
          child: ListView(
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(30.0),
              right: ScreenUtil().setWidth(30.0),
            ),
            children: <Widget>[
              SizedBox(height: ScreenUtil().setWidth(30.0)),
              _uploadIDCard(),
              SizedBox(height: ScreenUtil().setWidth(48.0)),
              Divider(
                color: Color(0xffcccccc),
                height: ScreenUtil().setWidth(1),
              ),
              SizedBox(height: ScreenUtil().setWidth(57.0)),
              _uploaddriverLicense(),
              SizedBox(height: ScreenUtil().setWidth(48.0)),
              Divider(
                color: Color(0xffcccccc),
                height: ScreenUtil().setWidth(1),
              ),
              SizedBox(height: ScreenUtil().setWidth(10.0)),
              fillInformation(),
              SizedBox(height: ScreenUtil().setWidth(50.0)),
              Offstage(
                offstage: Provider.of<DriverInformation>(context, listen: false)
                        .driverInfo
                        .driverMpdify !=
                    '0000',
                child: Text(
                  '请确认身份信息无误后提交',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(28.0),
                    color: Color(0xff3FB956),
                  ),
                ),
              ),
              SizedBox(height: ScreenUtil().setWidth(36.0)),
              Offstage(
                offstage: Provider.of<DriverInformation>(context, listen: false)
                        .driverInfo
                        .driverMpdify !=
                    '0000',
                child: MaterialButton(
                  color: Color(0xff3FB956),
                  elevation: 0,
                  disabledColor: Color.fromARGB(255, 188, 188, 188),
                  minWidth: ScreenUtil().setWidth(700.0),
                  height: ScreenUtil().setWidth(100.0),
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(50.0)),
                  ),
                  child: Text(
                    '提交',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(35.0),
                    ),
                  ),
                  onPressed: () => Request.submitCertificatesInfo(context),
                ),
              ),
              SizedBox(height: ScreenUtil().setWidth(80.0))
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _nameController.dispose();
    _cardIdController.dispose();
    super.dispose();
  }

  // 上传身份证
  Widget _uploadIDCard() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  width: ScreenUtil().setWidth(8),
                  height: ScreenUtil().setWidth(28),
                  decoration: BoxDecoration(
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(4)),
                    color: Color(0xff3FB956),
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(10)),
                Text(
                  '上传身份证照片',
                  style: TextStyle(
                      color: Color(0xff333333),
                      fontSize: ScreenUtil().setSp(30),
                      fontWeight: FontWeight.w500),
                ),
              ],
            ),
            Container(
              alignment: Alignment.topLeft,
              child: Text(
                '照片仅用于身份认证',
                style: TextStyle(
                  color: Color(0xff999999),
                  fontSize: ScreenUtil().setSp(28.0),
                ),
              ),
            ),
          ],
        ),
        SizedBox(height: ScreenUtil().setWidth(48.0)),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            // 身份证正面
            Consumer<DriverInformation>(builder: (context, state, child) {
              return _uploadedIDCard(
                  context,
                  'assets/icon/frontOfIDCard.png',
                  state.driverInfo.frontIdCard,
                  'frontOfIDCard',
                  state.driverInfo.driverMpdify);
            }),
            // 身份证反面
            Consumer<DriverInformation>(builder: (context, state, child) {
              return _uploadedIDCard(
                  context,
                  'assets/icon/backOfIDCard.png',
                  state.driverInfo.backIdCard,
                  'backOfIDCard',
                  state.driverInfo.driverMpdify);
            }),
          ],
        ),
      ],
    );
  }

  // 上传驾驶证照片
  Widget _uploaddriverLicense() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  width: ScreenUtil().setWidth(8),
                  height: ScreenUtil().setWidth(28),
                  decoration: BoxDecoration(
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(4)),
                    color: Color(0xff3FB956),
                  ),
                ),
                SizedBox(width: ScreenUtil().setWidth(10)),
                Text(
                  '上传驾驶证照片',
                  style: TextStyle(
                      color: Color(0xff333333),
                      fontSize: ScreenUtil().setSp(30),
                      fontWeight: FontWeight.w500),
                ),
              ],
            ),
            Container(
              alignment: Alignment.topLeft,
              child: Text(
                '照片仅用于司机认证',
                style: TextStyle(
                  color: Color(0xff999999),
                  fontSize: ScreenUtil().setSp(28.0),
                ),
              ),
            ),
          ],
        ),
        SizedBox(
          height: ScreenUtil().setWidth(48.0),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            // 驾驶证正面
            Consumer<DriverInformation>(builder: (context, state, child) {
              return _uploadedIDCard(
                  context,
                  'assets/icon/driverLicenseFront.png',
                  state.driverInfo.frontDriverLicense,
                  'driverLicenseFront',
                  state.driverInfo.driverMpdify);
            }),
            // 驾驶证背面
            Consumer<DriverInformation>(builder: (context, state, child) {
              return _uploadedIDCard(
                  context,
                  'assets/icon/driverLicenseBack.png',
                  state.driverInfo.backDriverLicense,
                  'driverLicenseBack',
                  state.driverInfo.driverMpdify);
            }),
          ],
        ),
      ],
    );
  }

  // 上传的证件
  Widget _uploadedIDCard(
      context, cardFile, iDCard, frontOfImgIDCard, driverMpdify) {
    return InkWell(
        child: Container(
          width: ScreenUtil().setWidth(330.0),
          height: ScreenUtil().setWidth(210.0),
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.circular(6.0),
          ),
          child: iDCard == null
              ? Image.asset(
                  cardFile,
                  width: ScreenUtil().setWidth(330.0),
                  height: ScreenUtil().setWidth(210.0),
                )
              : Stack(
                  children: <Widget>[
                    Image.network(
                      iDCard,
                      width: ScreenUtil().setWidth(330.0),
                      height: ScreenUtil().setWidth(210.0),
                    ),
                    Positioned(
                        bottom: 0,
                        left: 0,
                        child: Container(
                            child: Offstage(
                          offstage: driverMpdify != '0000',
                          child: Container(
                            alignment: Alignment.center,
                            width: ScreenUtil().setWidth(330.0),
                            height: ScreenUtil().setWidth(45.0),
                            decoration: BoxDecoration(
                              color: Color(0x99000000),
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(6.0),
                                bottomRight: Radius.circular(6.0),
                              ),
                            ),
                            child: Text(
                              '上传成功 点击修改',
                              style: TextStyle(
                                color: Color(0xffffffff),
                                fontSize: ScreenUtil().setSp(22.0),
                              ),
                            ),
                          ),
                        ))),
                  ],
                ),
        ),
        onTap: () {
          print("1111111");
          if (driverMpdify != '0000') return;
          print("22222222");
          Utils.imageUpload(context).then((value) {
            print("444444------" + value);
            switch (frontOfImgIDCard) {
              case 'frontOfIDCard': // 身份证正面
                Provider.of<DriverInformation>(context, listen: false)
                    .setDriverInformation(frontIdCard: value);
                break;
              case 'backOfIDCard': // 身份证背面
                Provider.of<DriverInformation>(context, listen: false)
                    .setDriverInformation(backIdCard: value);
                break;
              case 'driverLicenseFront': // 驾驶证正面
                Provider.of<DriverInformation>(context, listen: false)
                    .setDriverInformation(frontDriverLicense: value);
                break;
              case 'driverLicenseBack': // 驾驶证背面
                Provider.of<DriverInformation>(context, listen: false)
                    .setDriverInformation(backDriverLicense: value);
                break;
              default:
            }
          });
        });
  }

  // 填写个人信息
  Widget fillInformation() {
    DriverInformation provider = Provider.of<DriverInformation>(context);
    // _nameController.text = provider.driverInfo.driverName;
    // _cardIdController.text = provider.driverInfo.idNo;
    return Column(
      children: <Widget>[
        Container(
          height: ScreenUtil().setWidth(120.0),
          child: TextField(
            textAlign: TextAlign.right,
            controller: _nameController,
            enabled: provider.driverInfo.driverMpdify == '0000',
            style: TextStyle(
                fontSize: ScreenUtil().setSp(32.0),
                color: Color(0xff333333)), //输入文本的样式
            decoration: InputDecoration(
              icon: Text(
                '真实姓名',
                style: TextStyle(
                  color: Color(0xff333333),
                  fontSize: ScreenUtil().setSp(30),
                ),
              ),
              hintText: provider.driverInfo.driverName.length > 0
                  ? provider.driverInfo.driverName
                  : '请输入真实姓名',
              hintStyle: TextStyle(
                fontSize: ScreenUtil().setSp(30),
                color: provider.driverInfo.driverName.length > 0
                    ? Color(0xff333333)
                    : Color(0xffB5B5B5),
              ),
              border: InputBorder.none,
            ),
          ),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)),
            ),
          ),
          padding: EdgeInsets.only(
            // bottom: ScreenUtil().setWidth(10),
            top: ScreenUtil().setWidth(30),
          ),
        ),
        Container(
          height: ScreenUtil().setWidth(120.0),
          child: TextField(
            textAlign: TextAlign.right,
            keyboardType: TextInputType.text,
            enabled: provider.driverInfo.driverMpdify == '0000',
            controller: _cardIdController,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(32.0),
              color: Color(0xff333333),
            ), //输入文本的样式
            decoration: InputDecoration(
              icon: Text(
                '身份证号码',
                style: TextStyle(
                  color: Color(0xff333333),
                  fontSize: ScreenUtil().setSp(30),
                ),
              ),
              hintText: provider.driverInfo.idNo.length > 0
                  ? provider.driverInfo.idNo
                  : '请输入身份证号码',
              hintStyle: TextStyle(
                fontSize: ScreenUtil().setSp(30),
                color: provider.driverInfo.idNo.length > 0
                    ? Color(0xff333333)
                    : Color(0xffB5B5B5),
              ),
              border: InputBorder.none,
            ),
          ),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)),
            ),
          ),
          padding: EdgeInsets.only(
            // bottom: ScreenUtil().setWidth(10),
            top: ScreenUtil().setWidth(30),
          ),
        ),
        InkWell(
          child: Container(
            height: ScreenUtil().setWidth(120.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        '驾驶证初领证日期',
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontSize: ScreenUtil().setSp(30),
                        ),
                      ),
                      Text(
                        provider.driverInfo.dateTimeCertification != null
                            ? provider.driverInfo.dateTimeCertification
                            : '请选择日期',
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(32.0),
                          color:
                              provider.driverInfo.dateTimeCertification != null
                                  ? Color(0xff333333)
                                  : Color(0xffB5B5B5),
                        ),
                      ),
                    ],
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_right,
                  color: Color(0xffB5B5B5),
                ),
              ],
            ),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)),
              ),
            ),
            padding: EdgeInsets.only(
              // bottom: ScreenUtil().setWidth(10),
              top: ScreenUtil().setWidth(30),
            ),
          ),
          onTap: () {
            if (provider.driverInfo.driverMpdify != '0000') return;
            _dateCertification(context, 'dateTimeCertification');
          },
        ),
        InkWell(
          child: Container(
            height: ScreenUtil().setWidth(120.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        '驾驶证有效期',
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontSize: ScreenUtil().setSp(30),
                        ),
                      ),
                      Consumer<DriverInformation>(
                          builder: (context, state, child) {
                        return Text(
                          provider.driverInfo.termValidity != null
                              ? provider.driverInfo.termValidity
                              : '请选择日期',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(32.0),
                            color: provider.driverInfo.termValidity != null
                                ? Color(0xff333333)
                                : Color(0xffB5B5B5),
                          ),
                        );
                      }),
                    ],
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_right,
                  color: Color(0xffB5B5B5),
                ),
              ],
            ),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)),
              ),
            ),
            padding: EdgeInsets.only(
              // bottom: ScreenUtil().setWidth(10),
              top: ScreenUtil().setWidth(30),
            ),
          ),
          onTap: () {
            if (provider.driverInfo.driverMpdify != '0000') return;
            _validityDriverLicense(context, 'termValidity');
          },
        )
      ],
    );
  }

  // 设置驾驶证初领证日期
  _dateCertification(context, dataString) {
    DateTime now = new DateTime.now();
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime(now.year, now.month, now.day), onConfirm: (date) {
      var data = date.year.toString() +
          '-' +
          date.month.toString() +
          '-' +
          date.day.toString();
      print(data);
      Provider.of<DriverInformation>(context, listen: false)
          .setDriverInformation(dateTimeCertification: data);
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  // 设置驾驶证有效期
  _validityDriverLicense(context, dataString) {
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime(2099, 12, 31), onConfirm: (date) {
      var data = date.year.toString() +
          '-' +
          date.month.toString() +
          '-' +
          date.day.toString();
      print(data);
      Provider.of<DriverInformation>(context, listen: false)
          .setDriverInformation(termValidity: data);
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }
}
